package com.fasteat.menu.mapper;

import com.fasteat.menu.entity.Material;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author run
 * @since 2021-01-19
 */
public interface MaterialMapper extends BaseMapper<Material> {

    //获取菜谱的用料表
    @Select("SELECT mi.*,mm.dosage FROM t_material_info mi INNER JOIN t_menu_material mm WHERE mm.menu_id = #{menuId} AND mm.material_id = mi.id;")
    public List<Material> findMaterialsOfMenu(String menuId);

    //通过用料id添加用料到菜谱中
    @Insert("INSERT INTO t_menu_material VALUES(#{materialId},#{menuId},#{dosage});")
    public int addMaterialOfMenuById(String menuId,String materialId,String dosage);

    //通过用料名称添加用料到菜谱中
    @Insert("INSERT INTO t_menu_material VALUES((SELECT id FROM t_material_info WHERE material_name = #{materialName}),#{menuId},#{dosage});")
    public int addMaterialOfMenuByName(String menuId,String materialName,String dosage);

    //删除菜谱的所有的用料信息
    @Delete("DELETE FROM t_menu_material WHERE menu_id = #{menuId};")
    public int removeMaterialOfMenu(String menuId);

    //通过用料id删除菜谱中的用料
    @Delete("DELETE FROM t_menu_material WHERE menu_id = #{menuId} AND material_id = #{materialId}")
    public int removeMaterialOfMenuById(String menuId,String materialId);

    //通过用料名称删除菜谱中的用料
    @Delete("DELETE FROM t_menu_material WHERE menu_id = #{menuId} AND material_id = (SELECT id FROM t_material_info WHERE material_name = #{materialName});")
    public int removeMaterialOfMenuByName(String menuId,String materialName);

    //通过用料id修改菜谱用料的用量
    @Update("UPDATE t_menu_material SET dosage = #{dosage} WHERE menu_id = #{menuId} AND material_id = #{materialId}")
    public int updateDosageById(String menuId,String materialId,String dosage);

    //通过用料名称修改菜谱用料的用量
    @Update("UPDATE t_menu_material SET dosage = #{dosage} WHERE menu_id = #{menuId} AND material_id = (SELECT id FROM t_material_info WHERE material_name = #{materialName})")
    public int updateDosageByName(String menuId,String materialName,String dosage);
}
